<?php
	require('../plugin/baidu.php');
	$weburl="http://www.glili.cn";
	$bigimg="/apistatic/360200.jpg";
	$smallimg="/apistatic/200200.jpg";
	$showNum=3; //要展示的数量

	$sendTextTpl="<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType><![CDATA[text]]></MsgType>
				<Content><![CDATA[%s]]></Content>
				</xml>";
	$sendNewsTpl="<xml>
					<ToUserName><![CDATA[%s]]></ToUserName>
					<FromUserName><![CDATA[%s]]></FromUserName>
					<CreateTime>%s</CreateTime>
					<MsgType><![CDATA[news]]></MsgType>
					<ArticleCount>%d</ArticleCount>
					<Articles>
					%s
					</Articles>
					</xml>";

	$sendNewsItem="
					<item>
					<Title><![CDATA[%s]]></Title> 
					<Description><![CDATA[%s]]></Description>
					<PicUrl><![CDATA[%s]]></PicUrl>
					<Url><![CDATA[%s]]></Url>
					</item>
					";
	

	if (isset($_GET["echostr"]))
	{
		echo $_GET["echostr"];
		exit();
	}
	else
	{
		$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];
		if (!empty($postStr)){
			$res=simplexml_load_string($postStr);
			if ($res->MsgType!='text')
			{
				if ($res->MsgType=='event')
				{
					if ($res->Event=='subscribe')
					{
						echo sprintf($sendTextTpl,$res->FromUserName,$res->ToUserName,time(),"欢迎您关注莉莉云音乐，在下面发送您想要的歌曲名称就可以进行搜索了哟\n提示:由于微信浏览器限制，可能需要您手动点击播放按钮");
						exit('');
					}
				}
				echo 'success';
			}
			else
			{	
				$mserver=new mserver();
				$resraw=$mserver->search($res->Content);

				$resa=array_map(function($v){
					return ["txt"=>$v->songName.'-'.$v->artistName,"id"=>$v->songId,"pic"=>explode("@",$v->songPicRadio)[0]];
				},$resraw);

				if (count($resa)==0)
				{
					echo sprintf($sendTextTpl,$res->FromUserName,$res->ToUserName,time(),"对不起,没有查找到歌曲");
					exit('');
				}

				$resa=array_slice($resa,0,$showNum,true);
				$txt="";
				foreach ($resa as $key=>$value)
				{
					
					$pic=$key==0?$value['pic'].'@s_0,w_360':$value['pic'].'@s_0,w_200';
					$txt=$txt.sprintf($sendNewsItem,$value['txt'],'',$pic,$weburl.'/#/player/'.$value['id']);
				}

				$showNum=count($resa);

				if (count($resraw)>3){
					$txt=$txt.sprintf($sendNewsItem,'查看更多','',$weburl.$smallimg,$weburl.'/#/search/'.$res->Content);
					$showNum++;
				}
				
				
				echo sprintf($sendNewsTpl,$res->FromUserName,$res->ToUserName,time(),$showNum,$txt);
				
			}
		}
	}
?>

